Developing Web Applications with Haskell and Yesod by Michael Snoyman
Author:Michael Snoyman [Michael Snoyman]
Language: eng
Format: epub
Tags: COMPUTERS / Programming Languages / General
ISBN: 9781449316969
Publisher: O'Reilly Media
Published: 2012-04-22T16:00:00+00:00
Monad Transformers
Monads are like onions. Monads are not like cakes. Shrek, more or less
Before we get into the heart of Yesod’s monads, we need to understand a bit about monad transformers. (If you already know all about monad transformers, you can likely skip this section.) Different monads provide different functionality: Reader allows read-only access to some piece of data throughout a computation, Error allows you to short-circuit computations, and so on.
Oftentimes, however, you would like to be able to combine a few of these features together. After all, why not have a computation with read-only access to some settings variable, which could error out at any time? One approach to this would be to write a new monad like ReaderError, but this has the obvious downside of exponential complexity: you’ll need to write a new monad for every single possible combination.
Instead, we have monad transformers. In addition to Reader, we have ReaderT, which adds reader functionality to any other monad. So we could represent our ReaderError as (conceptually):
type ReaderError = ReaderT Error
In order to access our settings variable, we can use the ask function. But what about short-circuiting a computation? We’d like to use throwError, but that won’t exactly work. Instead, we need to lift our call into the next monad up. In other words:
throwError :: errValue -> Error lift . throwError :: errValue -> ReaderT Error
There are a few things you should pick up here:
A transformer can be used to add functionality to an existing monad.
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
The Mikado Method by Ola Ellnestam Daniel Brolund(21537)
Hello! Python by Anthony Briggs(20777)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(19224)
Dependency Injection in .NET by Mark Seemann(18842)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(18450)
Kotlin in Action by Dmitry Jemerov(18216)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(18081)
Adobe Camera Raw For Digital Photographers Only by Rob Sheppard(16946)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(16932)
Grails in Action by Glen Smith Peter Ledbrook(16059)
Sass and Compass in Action by Wynn Netherland Nathan Weizenbaum Chris Eppstein Brandon Mathis(13765)
Secrets of the JavaScript Ninja by John Resig & Bear Bibeault(11774)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(10624)
A Developer's Guide to Building Resilient Cloud Applications with Azure by Hamida Rebai Trabelsi(10590)
Jquery UI in Action : Master the concepts Of Jquery UI: A Step By Step Approach by ANMOL GOYAL(9703)
Hit Refresh by Satya Nadella(9098)
The Kubernetes Operator Framework Book by Michael Dame(8534)
Exploring Deepfakes by Bryan Lyon and Matt Tora(8357)
Robo-Advisor with Python by Aki Ranin(8302)